﻿
downloading_area_opt_res = {
    kStartNormal =  "ONEIMAGE:frame_bottom\\start_normal.png;Stretch;0;";
    kStartHover =   "ONEIMAGE:frame_bottom\\start_hover.png;Stretch;0;";
    kStartPress =   "ONEIMAGE:frame_bottom\\start_press.png;Stretch;0;";
    kStartDisable = "ONEIMAGE:frame_bottom\\start_disable.png;Stretch;0;";

    kPauseNormal =  "ONEIMAGE:frame_bottom\\pause_normal.png;Stretch;0;";
    kPauseHover =   "ONEIMAGE:frame_bottom\\pause_hover.png;Stretch;0;";
    kPausePress =   "ONEIMAGE:frame_bottom\\pause_press.png;Stretch;0;";
    kPauseDisable = "ONEIMAGE:frame_bottom\\pause_disable.png;Stretch;0;";

    kCancelNormal =  "ONEIMAGE:frame_bottom\\cancel_normal.png;Stretch;0;";
    kCancelHover =   "ONEIMAGE:frame_bottom\\cancel_hover.png;Stretch;0;";
    kCancelPress =   "ONEIMAGE:frame_bottom\\cancel_press.png;Stretch;0;";
    kCancelDisable = "ONEIMAGE:frame_bottom\\cancel_disable.png;Stretch;0;";

    kProgressBarNormal = "9IMAGES:frame_bottom\\progress_bar_on.png?1,1,6,4;frame_bottom\\progress_bar_on.png?7,1,11,4;frame_bottom\\progress_bar_on.png?12,1,17,4;frame_bottom\\progress_bar_on.png?1,5,6,7;frame_bottom\\progress_bar_on.png?7,5,11,7;frame_bottom\\progress_bar_on.png?12,5,17,7;frame_bottom\\progress_bar_on.png?1,8,6,11;frame_bottom\\progress_bar_on.png?7,8,11,11;frame_bottom\\progress_bar_on.png?12,8,17,11;Stretch;null;";
    kProgressBarBG =     "9IMAGES:frame_bottom\\progress_bar_off.png?1,1,6,4;frame_bottom\\progress_bar_off.png?7,1,11,4;frame_bottom\\progress_bar_off.png?12,1,17,4;frame_bottom\\progress_bar_off.png?1,5,6,7;frame_bottom\\progress_bar_off.png?7,5,11,7;frame_bottom\\progress_bar_off.png?12,5,17,7;frame_bottom\\progress_bar_off.png?1,8,6,11;frame_bottom\\progress_bar_off.png?7,8,11,11;frame_bottom\\progress_bar_off.png?12,8,17,11;Stretch;null;";
};

downloading_area =
{
    inner_log = function (msg, core_msg)
        base_utility.log(string.format("[downloading_area] %s", msg), core_msg);
    end;

    init = function()
        ierd_tgp.tpf_ui.Require("frame_bottom\\wg_frame_bottom\\progress_node_tips_area.lua");
    end;

    show_area = function(show)
        local downloading_area_ctn_id = ui_utility.get_ctrl_id_by_path(
                           wg_frame_bottom_view.bottom_downLoading_area_id_, "DownLoadingAreaCtn");
        if show == false then
            ui_utility.setproperty_by_id(downloading_area_ctn_id, "Visible", "false");
        else
            ui_utility.setproperty_by_id(downloading_area_ctn_id, "Visible", "true");
        end;
    end;

    show_cnt_right_area = function(show)
        local downloading_area_ctn_id = ui_utility.get_ctrl_id_by_path(
                           wg_frame_bottom_view.bottom_downLoading_area_id_, "DownLoadingAreaCtn");
        if show == false then
            ui_utility.setproperty_by_path(
                           downloading_area_ctn_id, "Cnt_Right", "Visible", "false");
        else
            ui_utility.setproperty_by_path(
                           downloading_area_ctn_id, "Cnt_Right", "Visible", "true");
        end;
    end;

    show_error_prompt_cnt_area = function(show)
        local downloading_area_ctn_id = ui_utility.get_ctrl_id_by_path(
                           wg_frame_bottom_view.bottom_downLoading_area_id_, "DownLoadingAreaCtn");
        if show == false then
            ui_utility.setproperty_by_path(
                       downloading_area_ctn_id, "Cnt_Right\\error_prompt_cnt", "Visible", "false");
        else
            ui_utility.setproperty_by_path(
                       downloading_area_ctn_id, "Cnt_Right\\error_prompt_cnt", "Visible", "true");
        end;
    end;

    show_progress_cnt_area = function(show)
        local downloading_area_ctn_id = ui_utility.get_ctrl_id_by_path(
                       wg_frame_bottom_view.bottom_downLoading_area_id_, "DownLoadingAreaCtn");
        if show == false then
            ui_utility.setproperty_by_path(
                       downloading_area_ctn_id, "Cnt_Right\\ProgressCtn", "Visible", "false");
        else
            ui_utility.setproperty_by_path(
                       downloading_area_ctn_id, "Cnt_Right\\ProgressCtn", "Visible", "true");
        end;
    end;

    hide_all_operate_btn = function()
        downloading_area.refresh_left_btn_status(false);
        downloading_area.refresh_right_btn_status(false);
        downloading_area.refresh_gift_btn_status(false);
    end;

    -- 左边按钮
    refresh_left_btn_status = function(show, enable, is_start, tips)
        local downloading_area_ctn_id = ui_utility.get_ctrl_id_by_path(
                           wg_frame_bottom_view.bottom_downLoading_area_id_, "DownLoadingAreaCtn");
        if is_start == true then
            ui_utility.setproperty_by_path(downloading_area_ctn_id, "Cnt_Right\\btn_left", 
                                     "Background", downloading_area_opt_res.kStartNormal);
            ui_utility.setproperty_by_path(downloading_area_ctn_id, "Cnt_Right\\btn_left", 
                                     "BackgroundHover", downloading_area_opt_res.kStartHover);
            ui_utility.setproperty_by_path(downloading_area_ctn_id, "Cnt_Right\\btn_left", 
                                     "BackgroundPressed", downloading_area_opt_res.kStartPress);
        else
            ui_utility.setproperty_by_path(downloading_area_ctn_id, "Cnt_Right\\btn_left", 
                                     "Background", downloading_area_opt_res.kPauseNormal);
            ui_utility.setproperty_by_path(downloading_area_ctn_id, "Cnt_Right\\btn_left", 
                                     "BackgroundHover", downloading_area_opt_res.kPauseHover);
            ui_utility.setproperty_by_path(downloading_area_ctn_id, "Cnt_Right\\btn_left", 
                                     "BackgroundPressed", downloading_area_opt_res.kPausePress);
        end;

        if tips ~= nil then
            ui_utility.setproperty_by_path(downloading_area_ctn_id, 
                                     "Cnt_Right\\btn_left", "TextTip", tips);  
        end;

        local disable = "false";
        if enable == false then
            disable = "true";
        end;
        ui_utility.setproperty_by_path(downloading_area_ctn_id, 
                                     "Cnt_Right\\btn_left", "Disable", disable);

        local visible = "false";
        if show == true then
            visible = "true";
        end;
        ui_utility.setproperty_by_path(downloading_area_ctn_id, 
                                     "Cnt_Right\\btn_left", "Visible", visible);
    end;
    
    -- 右边按钮
    refresh_right_btn_status = function(show, enable, tips)
        local downloading_area_ctn_id = ui_utility.get_ctrl_id_by_path(
                           wg_frame_bottom_view.bottom_downLoading_area_id_, "DownLoadingAreaCtn");
        
        local visible = "false";
        if show == true then
            visible = "true";
        end;
        ui_utility.setproperty_by_path(downloading_area_ctn_id, 
                                        "Cnt_Right\\btn_right", "Visible", visible);

        local disable = "false";
        if enable == false then
            disable = "true";
        end;
        ui_utility.setproperty_by_path(downloading_area_ctn_id, 
                                        "Cnt_Right\\btn_right", "Disable", disable);

        if tips ~= nil then
            ui_utility.setproperty_by_path(
                          downloading_area_ctn_id, "Cnt_Right\\btn_right", "TextTip", tips);  
        end;
    end;
    
    -- 礼包按钮
    refresh_gift_btn_status = function(show)
        local downloading_area_ctn_id = ui_utility.get_ctrl_id_by_path(
                           wg_frame_bottom_view.bottom_downLoading_area_id_, "DownLoadingAreaCtn");
        if show == true then
            ui_utility.setproperty_by_path(downloading_area_ctn_id, 
                            "Cnt_Right\\btn_gift", "Visible", "true");
        else
            ui_utility.setproperty_by_path(downloading_area_ctn_id, 
                            "Cnt_Right\\btn_gift", "Visible", "false");
        end
    end;

    -- 进度设置
    set_progress_info_helper = function(fProcess, status_txt, speed_txt, remain_time)
        downloading_area.show_area();
        downloading_area.show_cnt_right_area();
        downloading_area.show_progress_cnt_area();
        downloading_area.show_error_prompt_cnt_area(false);

        local downloading_area_ctn_id = ui_utility.get_ctrl_id_by_path(
                           wg_frame_bottom_view.bottom_downLoading_area_id_, "DownLoadingAreaCtn");
        ui_utility.setproperty_by_path(
                            downloading_area_ctn_id, "Cnt_Right\\ProgressCtn", "Visible", "true");
        ui_utility.setproperty_by_path(downloading_area_ctn_id, 
                            "Cnt_Right\\ProgressCtn\\ProgressBarCtn\\LvlProgressBarBackground", 
                             "Background", downloading_area_opt_res.kProgressBarBG);
        local p_lvl_progress_bar = ui_utility.get_ctrl_ptr_by_path(
                            downloading_area_ctn_id, 
                            "Cnt_Right\\ProgressCtn\\ProgressBarCtn\\LvlProgressBarBackground\\LvlProgressBar");
        if p_lvl_progress_bar ~= nil then
            if fProcess < 2 then
                fProcess = 0.0;
            end
            p_lvl_progress_bar:SetPos(fProcess, TRUE);
            local image_str = U8toCUIStr(downloading_area_opt_res.kProgressBarNormal);
            p_lvl_progress_bar:SetLevelTopImage(image_str);
            p_lvl_progress_bar:Show(TRUE);
        end
        -- 设置进度文本
        ui_utility.setproperty_by_path(downloading_area_ctn_id, 
                                    "Cnt_Right\\ProgressCtn\\Ctn_Info_Area", "Visible", "true");
        local status_txt_id = ui_utility.get_ctrl_id_by_path(downloading_area_ctn_id, 
                                    "Cnt_Right\\ProgressCtn\\Ctn_Info_Area\\status_txt");
        ui_utility.setproperty_by_id(status_txt_id, "Caption", status_txt);
        ui_utility.setproperty_by_id(status_txt_id, 
                                "TextColor", wg_frame_bottom_view.res_cfg_.DOWNLOADING_TXT_COLOR);
        -- 设置速度
        if speed_txt ~= nil and speed_txt ~= "" then
            ui_utility.setproperty_by_path(downloading_area_ctn_id, 
                                    "Cnt_Right\\ProgressCtn\\Ctn_Info_Area\\speed_txt", "Caption", 
                                    speed_txt);
            ui_utility.setproperty_by_path(downloading_area_ctn_id, 
                                    "Cnt_Right\\ProgressCtn\\Ctn_Info_Area\\speed_txt", 
                                    "TextColor", 
                                     wg_frame_bottom_view.res_cfg_.DOWNLOADING_TXT_COLOR);
            ui_utility.setproperty_by_path(downloading_area_ctn_id, 
                                    "Cnt_Right\\ProgressCtn\\Ctn_Info_Area\\speed_txt", "Visible", 
                                    "true");
        else
            ui_utility.setproperty_by_path(downloading_area_ctn_id, 
                                    "Cnt_Right\\ProgressCtn\\Ctn_Info_Area\\speed_txt", "Visible", 
                                    "false");
        end
        -- 设置剩余时间
        if remain_time ~= nil and remain_time ~= "" then
            ui_utility.setproperty_by_path(downloading_area_ctn_id, 
                                    "Cnt_Right\\ProgressCtn\\Ctn_Info_Area\\remain_time_txt", 
                                    "Caption", remain_time);
            ui_utility.setproperty_by_path(downloading_area_ctn_id, 
                                    "Cnt_Right\\ProgressCtn\\Ctn_Info_Area\\remain_time_txt", 
                                    "TextColor", 
                                     wg_frame_bottom_view.res_cfg_.DOWNLOADING_TXT_COLOR);
            ui_utility.setproperty_by_path(downloading_area_ctn_id, 
                                    "Cnt_Right\\ProgressCtn\\Ctn_Info_Area\\remain_time_txt", 
                                    "Visible", "true");
        else
            ui_utility.setproperty_by_path(downloading_area_ctn_id, 
                                    "Cnt_Right\\ProgressCtn\\Ctn_Info_Area\\remain_time_txt", 
                                    "Visible", "false");
        end
    end;

    get_speed_remain_time = function(fSpeed, fRemain_time)
        local speed_txt = "";
        local remain_time_txt = "";
        if fSpeed ~= nil and 0 ~= fSpeed then
            speed_txt = string.format("%s", state_operation_panel.format_speed(fSpeed));
        end
        if fRemain_time ~= nil then
            remain_time_txt = string.format("%s", 
                                        state_operation_panel.format_remain_time(fRemain_time));
        end
        return speed_txt, remain_time_txt;
    end;

    -- 设置 错误提示 状态
    set_error_prompt_txt = function(err_prompt_txt, game_state)
        downloading_area.show_area();
        downloading_area.show_cnt_right_area();
        downloading_area.show_progress_cnt_area(false);
        downloading_area.show_error_prompt_cnt_area();

        local downloading_area_ctn_id = ui_utility.get_ctrl_id_by_path(
                                    wg_frame_bottom_view.bottom_downLoading_area_id_, 
                                    "DownLoadingAreaCtn");
        
        ui_utility.setproperty_by_path(downloading_area_ctn_id, 
                                    "Cnt_Right\\error_prompt_cnt\\error_prompt_txt", 
                                    "ShowTip", "true");
        ui_utility.setproperty_by_path(downloading_area_ctn_id, 
                                    "Cnt_Right\\error_prompt_cnt\\error_prompt_txt", 
                                    "TextTip", err_prompt_txt);
        ui_utility.setproperty_by_path(downloading_area_ctn_id, 
                                    "Cnt_Right\\error_prompt_cnt\\error_prompt_txt", 
                                    "Caption", err_prompt_txt);
        MainCommon.SetButtonTextColor(ui_utility.uilib(),
                downloading_area_ctn_id, "Cnt_Right\\error_prompt_cnt\\error_prompt_txt", 
                wg_frame_bottom_view.res_cfg_.DOWNLOADING_TXT_COLOR);

        local btn_tips = "";
        if game_state == ierd_tgp.game_library.GAME_STATE_BROKEN then
            btn_tips = "重新下载";
        elseif game_state == ierd_tgp.game_library.GAME_STATE_UPDATE_FAIL then
            btn_tips = "重新更新";
        elseif game_state == ierd_tgp.game_library.GAME_STATE_DOWNLOADED then
            btn_tips = "安装";
        end;        

        if game_state ~= nil then
            downloading_area.refresh_left_btn_status(true, true, true, btn_tips);
            downloading_area.refresh_right_btn_status(false);
        else
            downloading_area.hide_all_operate_btn();
        end
    end;
    
    -- 设置 正在安装 状态
    set_installing_status = function(game_id, fProcess, support_silent_install, install_mode, 
                                    custom_text)
        -- 安装过程不显示礼包
        downloading_area.refresh_gift_btn_status(false);

        local downloading_area_ctn_id = ui_utility.get_ctrl_id_by_path(
                           wg_frame_bottom_view.bottom_downLoading_area_id_, "DownLoadingAreaCtn");

        if support_silent_install == true then
            local status_txt = LoadStr(ID_BT_INSTALLING);
            if fProcess ~= nil then
                local show_progress = fProcess;
                if install_mode == 1 then  -- 支持边下载边安装
                    local game_lib =
                        base_utility.get_component_adapter("IGame_library",
                        "ierd_tgp.game_library.Get_game_library_adapter();");
                    local init_install_progress = 0;
                    if game_lib ~= nil then
                        init_install_progress = game_lib:GetGameInitInstallProgress(game_id);
                    end
                    if fProcess >= init_install_progress and init_install_progress < 100 then
                        local comp_progress = 
                          100 * (fProcess - init_install_progress) / (100 - init_install_progress);
                        if comp_progress >= 0 and comp_progress <= 100 then
                            show_progress = comp_progress;
                        end
                    end
                end      
                local use_tinysetup = frame_bottom_model.get_game_install_mode(game_id);
                if use_tinysetup == 1 then
                    if custom_text ~= nil and custom_text ~= "" then  -- tinysetup 详细安装进度信息
                        status_txt = string.format("%s %.2f%%", 
                            LoadStr(ID_BT_INSTALLING).." "..custom_text, 
                            show_progress);
                    else
                        status_txt = string.format("%s %.2f%%", 
                            LoadStr(ID_BT_INSTALLING), show_progress);
                    end
                else
                local blocked_req={};
                blocked_req.game_id = game_id;
                local blocked_state = comm_center.call_service(
                                            "i_service_game_install_blocked", blocked_req);
                if blocked_state ~= nil and blocked_state.install_blocked == 1 then  
                    -- 安装卡顿，提示用户耐心等待
                    status_txt = string.format("%s %.2f%%", 
                            LoadStr("GAME_INSTALLING_BLOCKED"), show_progress);
                else
                    status_txt = string.format("%s %.2f%%", 
                            LoadStr(ID_BT_INSTALLING), show_progress);
                end
            end
            end
            downloading_area.set_progress_info_helper(fProcess, status_txt, "", "");
            -- 进度节点
            downloading_area.refresh_progress_node(game_id, fProcess);
            -- 设置左边按钮的状态
            downloading_area.refresh_left_btn_status(true, false);
            -- 设置右边按钮的状态
            downloading_area.refresh_right_btn_status(true, false);
        else
            -- 不支持静默安装，隐藏进度条
            downloading_area.show_area(false);
        end
    end;
    
    -- 设置 正在下载 状态
    set_downloading_status = function(fProcess, status_txt, speed_txt, remain_time_txt, game_id)
        downloading_area.set_progress_info_helper(fProcess, status_txt, speed_txt, remain_time_txt);

        -- 进度节点
        downloading_area.refresh_progress_node(game_id, fProcess);

        -- jameszhai: 天刀不显示更新取消按钮
        local   zParam = {};
        zParam["game_id"] = game_id;
        local zTemp = comm_center.call_service("Svr_GetHideCancelUpdate", zParam);
        if (nil ~= zTemp["need_hide"]) and (0 ~= zTemp["need_hide"]) then
            downloading_area.refresh_left_btn_status(false);
            downloading_area.refresh_right_btn_status(false);
        else
            local left_btn_enable = not downloading_area.is_in_installing_state(game_id);
            downloading_area.refresh_left_btn_status(
                                true, left_btn_enable, false, LoadStr(ID_SUSPEND));
            -- local is_base_install, install_type = downloading_area.is_basic_repo_installed(game_id);
            -- local right_btn_enable = ((not is_base_install) or (install_type == 2)) and
            --                          (not downloading_area.is_in_installing_state(game_id))
            downloading_area.refresh_right_btn_status(true, 
                    not downloading_area.is_in_installing_state(game_id));
        end;

        -- 显示礼包
        local show_download_gift = frame_bottom_model.get_show_download_gift(game_id);
        downloading_area.refresh_gift_btn_status(show_download_gift);
    end;

    -- 设置 下载暂停 状态
    set_download_pause_status = function(game_id, status_txt, fProcess)
        downloading_area.show_area();
        downloading_area.show_cnt_right_area();
        downloading_area.show_progress_cnt_area();
        downloading_area.show_error_prompt_cnt_area(false);

        local downloading_area_ctn_id = ui_utility.get_ctrl_id_by_path(
                           wg_frame_bottom_view.bottom_downLoading_area_id_, "DownLoadingAreaCtn");

        ui_utility.setproperty_by_path(downloading_area_ctn_id, 
                                        "Cnt_Right\\ProgressCtn", "Visible", "true");
        ui_utility.setproperty_by_path(downloading_area_ctn_id, 
                            "Cnt_Right\\ProgressCtn\\ProgressBarCtn\\LvlProgressBarBackground", 
                             "Background", downloading_area_opt_res.kProgressBarBG);
        local p_lvl_progress_bar = ui_utility.get_ctrl_ptr_by_path(downloading_area_ctn_id, 
                               "Cnt_Right\\ProgressCtn\\ProgressBarCtn\\LvlProgressBarBackground\\LvlProgressBar");
        if p_lvl_progress_bar ~= nil then
            --fixme 九宫格图片在初始时会重叠显示（三宫格在高DPI下有位置偏移）
            -- 当小于某进度时暂时不显示进度图片
            if fProcess < 2 then
                fProcess = 0.0;
            end
            p_lvl_progress_bar:SetPos(fProcess, TRUE);
            local const_image_str = U8toCUIStr(downloading_area_opt_res.kProgressBarNormal);
            p_lvl_progress_bar:SetLevelTopImage(const_image_str);
            p_lvl_progress_bar:Show(TRUE);

            -- 进度节点
            downloading_area.refresh_progress_node(game_id, fProcess);
        end

        -- 设置进度文本
        ui_utility.setproperty_by_path(downloading_area_ctn_id, 
                                        "Cnt_Right\\ProgressCtn\\Ctn_Info_Area","Visible", "true");
        local status_txt_id = ui_utility.get_ctrl_id_by_path(downloading_area_ctn_id, 
                                        "Cnt_Right\\ProgressCtn\\Ctn_Info_Area\\status_txt");
        ui_utility.setproperty_by_id(status_txt_id, "Caption", status_txt);
        ui_utility.setproperty_by_id(status_txt_id, 
                                "TextColor", wg_frame_bottom_view.res_cfg_.DOWNLOADING_TXT_COLOR);
        -- 设置速度
        ui_utility.setproperty_by_path(downloading_area_ctn_id, 
                                "Cnt_Right\\ProgressCtn\\Ctn_Info_Area\\speed_txt", 
                                "Caption", "");
        -- 设置剩余时间
        ui_utility.setproperty_by_path(downloading_area_ctn_id, 
                                "Cnt_Right\\ProgressCtn\\Ctn_Info_Area\\remain_time_txt", 
                                "Caption", "");


        -- jameszhai: 天刀不显示更新取消按钮
        local   zParam = {};
        zParam["game_id"] = game_id;
        local zTemp = comm_center.call_service("Svr_GetHideCancelUpdate", zParam);
        if (nil ~= zTemp["need_hide"]) and (0 ~= zTemp["need_hide"]) then
            downloading_area.refresh_left_btn_status(false);
            downloading_area.refresh_right_btn_status(false);
        else
            downloading_area.refresh_left_btn_status(true, true, true, LoadStr(ID_CONTINUE));
            -- local is_base_install, install_type = downloading_area.is_basic_repo_installed(game_id);
            -- local right_btn_enable = ((not is_base_install) or (install_type == 2));
            downloading_area.refresh_right_btn_status(true, true);
        end;

        -- 显示礼包
        local show_download_gift = frame_bottom_model.get_show_download_gift(game_id);
        downloading_area.refresh_gift_btn_status(show_download_gift);
    end;
 
    -- 设置 正在更新 状态
    set_updating_status = function(game_id, fProcess, status_txt, speed_txt, remain_time)
        downloading_area.set_progress_info_helper(fProcess, status_txt, speed_txt, remain_time);

        -- 进度节点
        downloading_area.refresh_progress_node(game_id, fProcess);

        -- jameszhai: 天刀不显示更新取消按钮
        local   zParam = {};
        zParam["game_id"] = game_id;
        local zTemp = comm_center.call_service("Svr_GetHideCancelUpdate", zParam);
        local bTemp = (nil ~= zTemp["need_hide"]) and (0 ~= zTemp["need_hide"]);
        if bTemp then
            downloading_area.refresh_right_btn_status(false);
        else
            local is_base_install, install_type = downloading_area.is_basic_repo_installed(game_id);
            downloading_area.refresh_right_btn_status(true, not is_base_install);
        end;

        -- 外部第三方更新方式
        local external_update = state_operation_panel.is_external_update(game_id);
        -- 设置左边暂停背景图
        -- 如果是多分支版本 或 第三方更新方式，则不显示暂停按钮
        local is_switch_branch_game = 
                                state_operation_panel.is_game_last_action_switch_branch(game_id);
        if is_switch_branch_game == 1 or external_update == 1 then
            downloading_area.refresh_left_btn_status(false);
        else
            if bTemp then
                downloading_area.refresh_left_btn_status(false);
            else
                downloading_area.refresh_left_btn_status(true, true, false, LoadStr(ID_SUSPEND));
            end;
        end

        -- 显示礼包
        local show_download_gift = frame_bottom_model.get_show_download_gift(game_id);
        downloading_area.refresh_gift_btn_status(show_download_gift);
    end;

    -- 设置 更新暂停 状态
    set_update_pause_status = function (game_id, status_txt, fProcess)
        downloading_area.show_area();
        downloading_area.show_cnt_right_area();
        downloading_area.show_progress_cnt_area();
        downloading_area.show_error_prompt_cnt_area(false);
       
        local downloading_area_ctn_id = ui_utility.get_ctrl_id_by_path(
                           wg_frame_bottom_view.bottom_downLoading_area_id_, "DownLoadingAreaCtn");
        -- 进度条
        ui_utility.setproperty_by_path(downloading_area_ctn_id, 
                                "Cnt_Right\\ProgressCtn\\ProgressBarCtn\\LvlProgressBarBackground", 
                                "Visible", "true");
        ui_utility.setproperty_by_path(downloading_area_ctn_id, 
                            "Cnt_Right\\ProgressCtn\\ProgressBarCtn\\LvlProgressBarBackground", 
                             "Background", downloading_area_opt_res.kProgressBarBG);
        local p_lvl_progress_bar = ui_utility.get_ctrl_ptr_by_path(downloading_area_ctn_id, 
                                "Cnt_Right\\ProgressCtn\\ProgressBarCtn\\LvlProgressBarBackground\\LvlProgressBar");
        if p_lvl_progress_bar ~= nil then
            if fProcess < 2 then
                fProcess = 0.0;
            end
            p_lvl_progress_bar:SetPos(fProcess, TRUE);
            local const_image_str = U8toCUIStr(downloading_area_opt_res.kProgressBarNormal);
            p_lvl_progress_bar:SetLevelTopImage(const_image_str);
            p_lvl_progress_bar:Show(TRUE);

            -- 进度节点
            downloading_area.refresh_progress_node(game_id, fProcess);
        end

        -- 状态描述 和 进度
        ui_utility.setproperty_by_path(downloading_area_ctn_id, 
                                    "Cnt_Right\\ProgressCtn\\Ctn_Info_Area", "Visible", "true");
        local status_txt_id = ui_utility.get_ctrl_id_by_path(downloading_area_ctn_id, 
                                    "Cnt_Right\\ProgressCtn\\Ctn_Info_Area\\status_txt");
        ui_utility.setproperty_by_id(status_txt_id, "Caption", status_txt);
        ui_utility.setproperty_by_id(status_txt_id, 
                                "TextColor", wg_frame_bottom_view.res_cfg_.DOWNLOADING_TXT_COLOR);
        -- 速度
        ui_utility.setproperty_by_path(downloading_area_ctn_id, 
                                    "Cnt_Right\\ProgressCtn\\Ctn_Info_Area\\speed_txt", 
                                    "Caption", "");
        -- 剩余时间
        ui_utility.setproperty_by_path(downloading_area_ctn_id, 
                                    "Cnt_Right\\ProgressCtn\\Ctn_Info_Area\\remain_time_txt", 
                                    "Caption", "");


        -- 操作按钮的状态
        -- jameszhai: 天刀不显示更新取消按钮
        local   zParam = {};
        zParam["game_id"] = game_id;
        local zTemp = comm_center.call_service("Svr_GetHideCancelUpdate", zParam);
        if (nil ~= zTemp["need_hide"]) and (0 ~= zTemp["need_hide"]) then
            downloading_area.refresh_left_btn_status(false);
            downloading_area.refresh_right_btn_status(false);
        else
            -- 左边按钮 开始
            downloading_area.refresh_left_btn_status(true, true, true, LoadStr(ID_CONTINUE));
            -- 右边按钮 取消
            local is_base_install, install_type = downloading_area.is_basic_repo_installed(game_id);
            downloading_area.refresh_right_btn_status(true, not is_base_install);
        end;

        -- 显示礼包
        local show_download_gift = frame_bottom_model.get_show_download_gift(game_id);
        downloading_area.refresh_gift_btn_status(show_download_gift);
    end;

    on_gift_btn_click = function()
        local game_id = wg_frame_bottom_view.game_and_plugin_state_tbl_.game.game_id;

        local gift_data = frame_bottom_model.get_download_gift_data(game_id);

        if gift_data == nil or gift_data.status == nil or 
           gift_data.url == nil or gift_data.url == "" then
            base_utility.log("game_info does not exist. data = " .. tostring(gift_data));
            return;
        end;

        if gift_data.status ~= 0 then
            -- 显示过则把标志位置为-1
            gift_data.status = -1;
            -- 拉起礼包页面
            if gift_data.url ~= nil then
                comm_center.call_service("i_service_open_link_confirm_launch", gift_data.url);
                --数据上报
                state_operation_panel.report_gift_operation(game_id, "click", gift_data.url);
            end;
        end;
    end;

    on_right_btn_click = function()
        if wg_frame_bottom_view.game_and_plugin_state_tbl_.game == nil then 
            return 
        end

        local game_id = wg_frame_bottom_view.game_and_plugin_state_tbl_.game.game_id;

        local state_value = wg_frame_bottom_view.game_and_plugin_state_tbl_.game.status;
        local sub_state_value = wg_frame_bottom_view.game_and_plugin_state_tbl_.game.sub_status;

        local ext_state = 
              wg_frame_bottom_view.game_and_plugin_state_tbl_.game.extension_game_state.iState;
        local ext_sub_state = 
              wg_frame_bottom_view.game_and_plugin_state_tbl_.game.extension_game_state.iSub_state;
        
        downloading_area.inner_log(
                    string.format("on_right_btn_click[%d, %d]", ext_state, ext_sub_state));
        
        local report_op_type = "";

        if ext_state == ierd_tgp.game_library.GAME_STATE_NEED_UPDATE then
            -- 更新操作，判断当前是暂停状态还是需要更新状态
            local pause_status = frame_bottom_model.get_game_update_pause_status(game_id);
            if 0 ~= pause_status then
                frame_bottom_control.excute_game_act(GameActions.kCancelUpdate, game_id);
                report_op_type ="cancel_update_game";    
            end
        elseif ext_state == ierd_tgp.game_library.GAME_STATE_UPDATING then
            -- 取消更新
            frame_bottom_control.excute_game_act(GameActions.kCancelUpdate, game_id);
            report_op_type = "cancel_update_game";
        elseif ext_state == ierd_tgp.game_library.GAME_STATE_DOWNLOAD_PAUSED then
            frame_bottom_control.excute_game_act(GameActions.kCancelDownload, game_id); 
            report_op_type = "cancel_download_game"; 
        elseif ext_state == ierd_tgp.game_library.GAME_STATE_DOWNLOADING then
            frame_bottom_control.excute_game_act(GameActions.kCancelDownload, game_id);
            report_op_type = "cancel_download_game";
        else
            base_utility.log(string.format("on_right_btn_click, ignore status:%d", ext_state));
        end
        -- 数据上报
        if report_op_type ~= "" then
            state_operation_panel.report_game_operation(game_id, report_op_type);
        end
    end;

    on_left_btn_click = function()
        -- 包含暂停, 继续操作
        if wg_frame_bottom_view.game_and_plugin_state_tbl_.game == nil then 
            return 
        end
        local game_id = wg_frame_bottom_view.game_and_plugin_state_tbl_.game.game_id;

        local state_value = wg_frame_bottom_view.game_and_plugin_state_tbl_.game.status;

        local ext_state = 
              wg_frame_bottom_view.game_and_plugin_state_tbl_.game.extension_game_state.iState;
        local ext_sub_state = 
              wg_frame_bottom_view.game_and_plugin_state_tbl_.game.extension_game_state.iSub_state;

        downloading_area.inner_log(
                    string.format("on_left_btn_click[%d, %d]", ext_state, ext_sub_state));

        local report_op_type = ""; 
        if ext_state == ierd_tgp.game_library.GAME_STATE_DOWNLOADING then
            -- 暂停下载操作
            frame_bottom_control.excute_game_act(GameActions.kPauseDownload, game_id);
            report_op_type = "pause_download_game";
        elseif ext_state == ierd_tgp.game_library.GAME_STATE_DOWNLOAD_PAUSED then
            -- 继续下载操作
            if state_operation_panel.is_allow_continue_operation_helper(game_id) then
                frame_bottom_control.excute_game_act(GameActions.kContinueDownload, game_id);
                report_op_type = "continue_download_game";
            end
        elseif ext_state == ierd_tgp.game_library.GAME_STATE_NEED_UPDATE then
            -- 继续更新操作
            local use_tinysetup = frame_bottom_model.get_game_update_mode(game_id);
            if use_tinysetup ~= 1 then
                frame_bottom_control.excute_game_act(GameActions.kUpdateGame, game_id);
            else
                --使用tinysetup的继续更新
                --frame_bottom_control.excute_game_act(GameActions.kContinueUpdate, game_id);
                local param = {};
                param["game_id"] = game_id;
                comm_center.call_service("i_service_continue_update_game", param);
            end
            report_op_type = "update_game";
        elseif ext_state == ierd_tgp.game_library.GAME_STATE_UPDATING then
            -- 暂停更新
            frame_bottom_control.excute_game_act(GameActions.kPauseUpdate, game_id);
            report_op_type = "pause_update_game";
        elseif ext_state == ierd_tgp.game_library.GAME_STATE_BROKEN then
            -- 游戏损坏， 重新下载
            if state_operation_panel.is_allow_continue_operation_helper(game_id) then
                frame_bottom_control.excute_game_act(GameActions.kDownloadGame, game_id);
                report_op_type = "download_game";
            end
        elseif ext_state == ierd_tgp.game_library.GAME_STATE_UPDATE_FAIL then
            -- 更新失败， 重新更新 
            if state_operation_panel.is_allow_continue_operation_helper(game_id) then
                frame_bottom_control.excute_game_act(GameActions.kUpdateGame, game_id, server_id);
                report_op_type = "update_game";
            end
        elseif ext_state == ierd_tgp.game_library.GAME_STATE_DOWNLOADED then
            -- 已下载， 安装游戏
            if state_operation_panel.is_allow_continue_operation_helper(game_id) then
                frame_bottom_control.excute_game_act(GameActions.kInstallGame, game_id);
                report_op_type = "install_game";
            end
        elseif ext_state == ierd_tgp.game_library.GAME_STATE_DOWNLOAD_FAIL then
            -- 下载失败， 重新下载
            if state_operation_panel.is_allow_continue_operation_helper(game_id) then
                frame_bottom_control.excute_game_act(GameActions.kDownloadGame, game_id);
                report_op_type = "download_game";
            end
        end
        if report_op_type ~= "" then
            state_operation_panel.report_game_operation(game_id, report_op_type);
        end
    end;

    -- 基础包是否已经安装
    is_basic_repo_installed = function(game_id)
        local ret = false;
        local install_type = 0;
        local info = comm_center.call_service("i_service_tinysetup_repo_extension_info",
                                              {game_id = game_id});
        if info == nil or info.result == false or 
           info.repo_pkgs == nil or type(info.repo_pkgs) ~= "table" then
            return ret, install_type;
        end;
        if info.repo_install_type ~= nil then
            install_type = info.repo_install_type;
        end;
        local repo_pkgs_count = #(info.repo_pkgs);
        if repo_pkgs_count ~= 0 and info.repo_pkgs[1] ~= nil and
           info.repo_pkgs[1].ready ~= nil and info.repo_pkgs[1].ready == true then
            ret = true;
        end;
        return ret, install_type;
    end;

    -- 是否处于正在安装状态（子状态/主状态）
    is_in_installing_state = function(game_id)
        local ret = false;
        if wg_frame_bottom_view.game_and_plugin_state_tbl_ ~= nil and
           wg_frame_bottom_view.game_and_plugin_state_tbl_.game ~= nil then
            local game_state_param = wg_frame_bottom_view.game_and_plugin_state_tbl_.game;
            local state_value = game_state_param.status;
            local ext_state = game_state_param.extension_game_state.iState;
            if game_state_param.game_id == game_id and
               state_value == ierd_tgp.game_library.GAME_STATE_INSTALLING or
               ext_state == ierd_tgp.game_library.GAME_STATE_INSTALLING then
                ret = true;
            end;
        end;
        return ret;
    end;

    on_progress_bar_pos_change = function()
        if progress_node_tips_area ~= nil then
            progress_node_tips_area.on_progress_bar_pos_change(wg_frame_bottom_view.game_id_);
        end;
    end;

    refresh_progress_node = function(game_id, fProcess)
        local progress_bar_ctn = ui_utility.get_ctrl_id_by_path(
                           wg_frame_bottom_view.bottom_downLoading_area_id_, 
                           "DownLoadingAreaCtn\\Cnt_Right\\ProgressCtn\\ProgressBarCtn");
        progress_node_tips_area.show_progress_node(progress_bar_ctn, game_id);
        progress_node_tips_area.refresh_progress_node_status(game_id, fProcess);
    end;

    refresh_progress_bar_img = function()
        local p_lvl_progress_bar = ui_utility.get_ctrl_ptr_by_path(downloading_area_ctn_id, 
                                "Cnt_Right\\ProgressCtn\\ProgressBarCtn\\LvlProgressBarBackground\\LvlProgressBar");
        if p_lvl_progress_bar ~= nil then
            local const_image_str = U8toCUIStr(downloading_area_opt_res.kProgressBarNormal);
            p_lvl_progress_bar:SetLevelTopImage(const_image_str);
        end
    end;
};

downloading_area.init();